(function() {

    var
    ns = {
        i18n : ipoevents.namespace('i18n')
    },
    $ = ipoevents.jQuery;

    ns.i18n.transliterator = {
        
        russianEnglishHash : {
            'А' : 'A', 'Б' : 'B', 'В' : 'V', 'Г' : 'G', 'Д' : 'D', 'Е' : 'E', 'Ё' : 'Yo', 'Ж' : 'Zh',
            'З' : 'Z', 'И' : 'I', 'Й' : 'J', 'К' : 'K', 'Л' : 'L', 'М' : 'M', 'Н' : 'N', 'О' : 'O',
            'П' : 'P', 'Р' : 'R', 'С' : 'S', 'Т' : 'T', 'У' : 'U', 'Ф' : 'F', 'Х' : 'H', 'Ц' : 'Ts',
            'Ч' : 'Ch', 'Ш' : 'Sh', 'Щ' : 'Shch', 'Ъ' : '\'', 'Ы' : 'I', 'Ь' : '\'', 'Э' : 'E', 'Ю' : 'Yu', 'Я' : 'Ya'
        },

        englishRussianHash : {
            'A' : 'А', 'B' : 'Б', 'C' : 'К', 'D' : 'Д', 'E' : 'Е', 'F' : 'Ф', 'G' : 'Г', 'H' : 'Х', 'I' : 'И',
            'J' : 'Й', 'K' : 'К', 'L' : 'Л', 'M' : 'М', 'N' : 'Н', 'O' : 'О', 'P' : 'П', 'Q' : 'Я', 'R' : 'Р',
            'S' : 'С', 'T' : 'Т', 'U' : 'У', 'V' : 'В', 'W' : 'В', 'X' : 'Х', 'Y' : 'АЙ', 'Z' : 'З'
        },

        valuesHash : { },

        generateFullHash : function(hash) {
            var key, fullHash = { };

            for (key in hash) {
                var value = hash[key], lowerKey = key.toLowerCase();

                fullHash[key] = value;
                fullHash[lowerKey] = value.toLowerCase();
            }

            return fullHash;
        },

        initialize : function() {
            this.russianEnglishHash = this.generateFullHash(this.russianEnglishHash);
            this.englishRussianHash = this.generateFullHash(this.englishRussianHash);
        },

        encodeFromRussianToEnglish : function(text) {
            var self = this;

            return text.replace(/([\u0410-\u0451])/g, function(letter) {
                var translit = self.russianEnglishHash[letter];
                return ((translit != undefined) ? translit : '');
			});
        },

        encodeFromEnglishToRussian : function(text) {
            var self = this;

            return text.replace(/([A-z])/g, function(letter) {
                var translit = self.englishRussianHash[letter];
                return ((translit != undefined) ? translit : '');
			});
        },

        encodeFromCurrentLanguageToOther : function(text) {
            if (ns.i18n.locale.language == 'en') return this.encodeFromEnglishToRussian(text);
            else return this.encodeFromRussianToEnglish(text);
        },

        attachEncoderEventsHandler : function(masterField, slaveField, encodeFunction) {
            var self = this;

            masterField.bind('change keyup', function(e, o) {
                if (o && o.skipTransliterate) return;

                var slaveFieldValue = slaveField.val();

                if ((slaveFieldValue != '') && (self.valuesHash[slaveField] != slaveFieldValue)) return;

                var encodedValue = encodeFunction.call(self, masterField.val());
                slaveField.val(encodedValue);
                self.valuesHash[slaveField] = encodedValue;
                slaveField.trigger('change', { skipTransliterate : true });
            });
        },

        setEncoder : function() {
            for (var i = 0; i < arguments.length; ++i) {
                var
                russianField = $((arguments[i])[0]),
                englishField = $((arguments[i])[1]);

                this.attachEncoderEventsHandler(russianField, englishField, this.encodeFromRussianToEnglish);
                this.attachEncoderEventsHandler(englishField, russianField, this.encodeFromEnglishToRussian);
            }
        }

    };

    ipoevents.initializeManager.addInitializingObject(ns.i18n.transliterator);

})();
